package com.his.struts.dao.medicine;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.hospitalInfo.HospitalInfoImp;
import com.his.struts.form.MedicineForm;
import com.his.struts.util.DbUtil;
/**
 * 药物类
 * @author liuyan
 *
 */
public class MedicineImp implements Medicine {
	Logger log = Logger.getLogger(MedicineImp.class);
	static DbUtil db = new DbUtil();
	/**
	 * 得到所有的药物中药信息
	 * @return List<MedicineForm>
	 */
	public List<MedicineForm> getAllMedicine() {
		String sql = "select * from Medicine";
		List<MedicineForm> list = new ArrayList<MedicineForm>();
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while(rs.next()){
				MedicineForm mdf = new MedicineForm();
				mdf.setMid(rs.getInt("mdid"));
				mdf.setMprix(rs.getDouble("mprix"));
				mdf.setMsynopsis(rs.getString("msynopsis"));
				mdf.setMtype(rs.getInt("mtype"));
				mdf.setMedicine(rs.getString("medicine"));
				list.add(mdf);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}finally{
			db.closeAll();
		}
		return list;
	}
	
	/**
	 * 添加一种药物
	 * @return boolean 看是否添加成功
	 */
	public boolean addMedicine(MedicineForm mdf){
		boolean ok = false;
		boolean checkok = checkMedicine(mdf.getMedicine());
		if(checkok){
			String sql = "insert into Medicine (mtype,medicine,msynopsis,mprix) values (?,?,?,?)";
			ArrayList list = new ArrayList();
			list.add(mdf.getMtype());
			list.add(mdf.getMedicine());
			list.add(mdf.getMsynopsis());
			list.add(mdf.getMprix());
			int i = db.executeUpdate(sql, list);
			if(i == 1){
				ok = true;
			}
		}
		db.closeAll();
		return ok ;
	}
	
	/**
	 * 更新一种药物
	 * @return boolean 看是否添加成功
	 */
	public boolean updateMedicine(MedicineForm mdf){
		boolean ok = false;
		String sql = "update Medicine set mtype=?,medicine=?,msynopsis=?,mprix=? where mdid = ?";
		ArrayList list = new ArrayList();
		list.add(mdf.getMtype());
		list.add(mdf.getMedicine());
		list.add(mdf.getMsynopsis());
		list.add(mdf.getMprix());
		list.add(mdf.getMid());
		int i = db.executeUpdate(sql, list);
		if(i == 1){
			ok = true;
		}
		db.closeAll();
		return ok ;
	}
	
	/**
	 * 查看一种药是否存在一种药物
	 * @return boolean 是否存在
	 */
	public boolean checkMedicine(String medicinename){
		boolean ok = true;
		List<MedicineForm> list = this.getAllMedicine();
		for (MedicineForm medicineForm : list) {
			if(medicinename.equals(medicineForm.getMedicine())){
				ok = false;
				break;
			}
		}
		db.closeAll();
		return ok;
	}
	/**
	 * 删除一种药物
	 * @return boolean 看是否添加成功
	 */
	public boolean delMedicine(int mdid){
		boolean ok = false;
		String sql = "delete from Medicine where mdid = ?";
		ArrayList list = new ArrayList();
		list.add(mdid);
		int i = db.executeUpdate(sql, list);
		if(i == 1){
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 封装所有的药物中药信息
	 * @return List<MedicineForm>
	 */
	public List<MedicineForm> getMedicineBean(ResultSet rs) {
		List<MedicineForm> list = new ArrayList<MedicineForm>();
		try {
			while(rs.next()){
				MedicineForm mdf = new MedicineForm();
				mdf.setMid(rs.getInt("mdid"));
				mdf.setMprix(rs.getDouble("mprix"));
				mdf.setMsynopsis(rs.getString("msynopsis"));
				mdf.setMtype(rs.getInt("mtype"));
				mdf.setMedicine(rs.getString("medicine"));
				list.add(mdf);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}finally{
			db.closeAll();
		}
		return list;
	}
	
}
